package com.kotlinvben.controller

import com.kotlinvben.bean.Result
import com.kotlinvben.entity.Menu
import com.kotlinvben.service.MenuService
import com.kotlinvben.vo.MenuCreateVO
import com.kotlinvben.vo.MenuUpdateVO
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/system/menu")
class MenuController(
    private val menuService: MenuService
) {
    @PostMapping
    fun createMenu(@Valid @RequestBody menuCreateVO: MenuCreateVO): Result<Menu> {
        return Result.success(menuService.createMenu(menuCreateVO))
    }

    @PutMapping("/{id}")
    fun updateMenu(
        @PathVariable id: Long,
        @Valid @RequestBody menuUpdateVO: MenuUpdateVO
    ): Result<Menu> {
        return Result.success(menuService.updateMenu(id, menuUpdateVO))
    }

    @DeleteMapping("/{id}")
    fun deleteMenu(@PathVariable id: Long): Result<Unit> {
        menuService.deleteMenu(id)
        return Result.success()
    }

    @GetMapping("/{id}")
    fun getMenuById(@PathVariable id: Long): Result<Menu> {
        return Result.success(menuService.getMenuById(id))
    }

    @GetMapping("/list")
    fun getMenus(): Result<List<Menu>> {
        return Result.success(menuService.getMenus())
    }

    @GetMapping("/path-exists")
    fun pathExists(id: Long, path: String): Result<Boolean> {
        return Result.success(menuService.pathExists(id, path))
    }

    @GetMapping("/name-exists")
    fun nameExists(id: Long, name: String): Result<Boolean> {
        return Result.success(menuService.nameExists(id, name))
    }

}